package org.example.controller;

import dto.AgingTestQueryDTO;
import dto.InspectionQualityQueryDTO;
import entity.AgingTest;
import entity.QualityInspection;
import entity.QualityStandard;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.constant.MessageConstant;
import org.example.result.PageResult;
import org.example.result.Result;
import org.example.service.InspectorService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "质检员控制层")
@RequestMapping("/inspector")
@RequiredArgsConstructor
public class InspectorController {

    private final InspectorService inspectorService;

    /**
     * 查询质检单
     *
     * @param pageNumber
     * @param pageSize
     * @param inspectStatus
     * @param inspectCategory
     * @return
     */
    @Operation(description = "查询质检单")
    @GetMapping("/queryQualityInspection")
    public Result<PageResult> queryQualityInspection(
            @RequestParam(name = "pageNumber") Integer pageNumber,
            @RequestParam(name = "pageSize") Integer pageSize,
            @RequestParam(name = "inspectType") String inspectType,
            @RequestParam(name = "inspectStatus", required = false) String inspectStatus,
            @RequestParam(name = "inspectCategory", required = false) String inspectCategory,
            @RequestParam(name = "inspectionId", required = false) Integer inspectionId) {
        log.info("查询质检单：page = {}, pageSize = {}, inspectType = {} ,inspectStatus = {}, inspectCategory = {}, inspectionId = {}", pageNumber, pageSize, inspectType,
                inspectStatus, inspectCategory, inspectionId);
        InspectionQualityQueryDTO queryDTO = InspectionQualityQueryDTO.builder()
                .pageNumber(pageNumber)
                .pageSize(pageSize)
                .inspectStatus(inspectStatus)
                .inspectCategory(inspectCategory)
                .inspectType(inspectType)
                .inspectionId(inspectionId)
                .build();
        PageResult pageResult = inspectorService.queryQualityInspection(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 更新质检单
     *
     * @param qualityInspection
     * @return
     */
    @Operation(description = "更新质检单")
    @PutMapping("/updateQualityInspection")
    public Result<String> updateQualityInspection(@RequestBody QualityInspection qualityInspection) {
        log.info("更新质检单：qualityInspection = {}", qualityInspection);
        inspectorService.updateQualityInspection(qualityInspection);
        return Result.success(MessageConstant.UPLOAD_SUCCESS);
    }

    /**
     * 分页查询老化测试
     *
     * @param pageNumber
     * @param pageSize
     * @param testStatus
     * @param productName
     * @return
     */
    @Operation(description = "分页查询老化测试")
    @GetMapping("/queryAgingTest")
    public Result<PageResult> queryAgingTest(
            @RequestParam(name = "pageNumber") Integer pageNumber,
            @RequestParam(name = "pageSize") Integer pageSize,
            @RequestParam(name = "testStatus", required = false) String testStatus,
            @RequestParam(name = "productName", required = false) String productName) {
        log.info("分页查询老化测试：page = {}, pageSize = {}, testStatus = {}, productName = {}",
                pageNumber, pageSize, testStatus, productName);
        AgingTestQueryDTO queryDTO = AgingTestQueryDTO.builder()
                .pageNumber(pageNumber)
                .pageSize(pageSize)
                .testStatus(testStatus)
                .productName(productName)
                .build();
        PageResult pageResult = inspectorService.queryAgingTest(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 添加老化测试
     *
     * @param agingTest
     * @return
     */
    @Operation(description = "添加老化测试")
    @PostMapping("/addAgingTest")
    public Result<String> addAgingTest(@RequestBody AgingTest agingTest) {
        log.info("添加老化测试：agingTest = {}", agingTest);
        inspectorService.addAgingTest(agingTest);
        return Result.success();
    }

    /**
     * 更新老化测试
     *
     * @param agingTest
     * @return
     */
    @Operation(description = "更新老化测试")
    @PutMapping("/updateAgingTest")
    public Result<String> updateAgingTest(@RequestBody AgingTest agingTest) {
        log.info("更新老化测试：agingTest = {}", agingTest);
        inspectorService.updateAgingTest(agingTest);
        return Result.success(MessageConstant.UPLOAD_SUCCESS);
    }

    /**
     * 成功完成老化测试
     * @param agingTest
     * @return
     */
    @Operation(description = "成功完成")
    @PutMapping("/successAgingTest")
    public Result<String> successAgingTest(@RequestBody AgingTest agingTest) {
        log.info("成功完成：agingTest = {}", agingTest);
        inspectorService.successAgingTest(agingTest);
        return Result.success();
    }

    /**
     * 查询质检标准
     * @param inspectCategory
     * @return
     */
    @Operation(description = "查询质检标准")
    @GetMapping("/listQualityStandard")
    public Result<List<QualityStandard>> listQualityStandard(@RequestParam String inspectCategory) {
        log.info("查询质检标准：inspectCategory = {}", inspectCategory);
        List<QualityStandard> qualityStandardList = inspectorService.listQualityStandard(inspectCategory);
        return Result.success(qualityStandardList);
    }
}
